<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="550" draggable destroy-on-close :close-on-click-modal="false" @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="90px">
			<el-form-item label="字典名称" prop="dictName">
				<el-input v-model="form.dictName" placeholder="字典名称，例如：学历" />
			</el-form-item>
			<el-form-item label="字典类型" prop="dictType">
				<el-input v-model="form.dictType" :disabled="isDisabled" placeholder="字典类型，例如：app_edu_type" />
			</el-form-item>
			<el-form-item label="状态" prop="status" align="right">
				<jd-radio v-model="form.status" dict="sys_normal_disable"></jd-radio>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" type="textarea" placeholder=""></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
			<el-button @click="visible=false" >取 消</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增字典',
					edit: '编辑字典'
				},
				visible: false,
				isSaveing: false,
				form: {
					dictId:"",
					dictName: "",
					dictType: "",
					status: "0",
					remark: undefined
				},
				isDisabled: false,
				rules: {
					dictName: [
						{required: true, message: '请输入字典名称'}
					],
					dictType: [
						{required: true, message: '请输入字典类型'}
					]
				},
				dic: [],
				dicProps: {
					value: "id",
					label: "name",
					emitPath: false,
					checkStrictly: true
				}
			}
		},
		mounted() {

		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				this.disabled = false;
				return this;
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res = {}
						if (this.mode == 'add'){
							res = await this.$API.system.dict.type.addType.post(this.form);
						} else if (this.mode == 'edit') {
							res = await this.$API.system.dict.type.editType.put(this.form);
						}
						this.isSaveing = false;
						console.log(res)
						if(res.code == 200){
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.msg, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				Object.assign(this.form, data);
				this.isDisabled = true;
			}
		}
	}
</script>

<style>

</style>
